<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script>
    <style type="text/css">
        .header {
            padding: 2.5rem 0.5rem 2rem 0.5rem;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="padding-top:25px;">
        <a class="aui-pull-left aui-btn" onclick="closeWin()"></a>
        <div class="aui-title">可大金服OA</div>
        <a class="aui-pull-rsight aui-btn"></a>
    </header>

    <div id="list"></div>

    <div id="list2"></div>



    <!-- <div class="foot-t"></div>
<ul class="foot">
  <li class="active">
    <i class="aui-iconfont aui-icon-home"></i>
    <p>首页</p>
  </li>
  <li>
    <i class="aui-iconfont aui-icon-my"></i>
    <p>我的</p>
  </li>
</ul> -->

</body>

<script type="text/template" id="template">
    <div class="menu">
        <ul>
            {{~it:value:index}}
            <!-- <li class="active">分类二</li> -->
            <li id="type{{=value.id}}" class="li_index" onclick="changeType({{=value.id}});" style="text-align:center">{{=value.name}}</li>
            {{~}}
        </ul>
    </div>
</script>

<script type="text/template" id="template2">

    <div class="index">
        <ul>
            {{~it:value:index}}
            <li onclick="readCon('{{=value.id}}','{{=value.end_time}}','{{=value.user_id}}','{{=value.user_name}}','{{=value.number}}');">
                <p>{{=value.name}}

                  {{? value.red_head==1}}
                  <i class="aui-bg-danger">红头</i>
                  {{??}}{{?}}

                  {{? value.grade==1}}i
                  <i class="aui-bg-primary">{{=value.grade}}级文件</i>
                  {{?? value.grade==2}}
                  <i class="aui-bg-success">{{=value.grade}}级文件</i>
                  {{?? value.grade==3}}
                  <i class="aui-bg-info">{{=value.grade}}级文件</i>
                  {{??}}
                  {{?}}

                  {{? value.secret==1}}
                  <i class="aui-bg-primary">{{=value.secret}}级机密</i>
                  {{?? value.secret==2}}
                  <i class="aui-bg-success">{{=value.secret}}级机密</i>
                  {{?? value.secret==3}}
                  <i class="aui-bg-info">{{=value.secret}}级机密</i>
                  {{??}}
                  {{?}}
                </p>
                <span id="create_time">{{=value.create_time}}</span>
            </li>
            {{~}}

        </ul>
    </div>
</script>

<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript">
    var typeid = '';
    var page = 0;
    var p = 0;

    function openClub() {
        var delay = 0;
        if (api.systemType != 'ios') {
            delay = 300;
        }
        api.openWin({
            name: 'club_win',
            url: './html/club_win.html',
            bounces: false,
            delay: delay,
            slidBackEnabled: true,
            vScrollBarEnabled: false,
            progress: {
                type: "page"
            }
        });
    }

    function closeWin() {
        api.closeWin({});
    }

    apiready = function() {
        updateData();
        addObserver();
        down_refresh();

    }

    function updateData() {
        var userid = $api.getStorage('id');
        api.ajax({
            url: 'http://oa.big-wealth.com/index.php?m=&c=SystemFolder&a=index_api&api_auth_id=' + userid,
            method: 'get',
        }, function(ret, err) {
            if (ret) {
                // alert( JSON.stringify( ret ) );
                // console.log( JSON.stringify( ret.date ) );
                typeid = ret.date[0].id;
                templateFun(ret.date);

            } else {
                alert( JSON.stringify( err ) );
            }
        });

    }

    function templateFun(data) {
        var template = $api.byId('template');
        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var list = $api.byId('list');
        $api.html(list, html);

        $("#type" + typeid).addClass("active");
        updateData2();
    }

    function updateData2(n) {
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '努力加载中...',
            text: '先喝杯茶...',
            modal: false
        });
        var userid = $api.getStorage('id');
        var url = 'http://oa.big-wealth.com/index.php?m=&c=info&a=my_info_api&api_auth_id=' + userid + '&f_id=' + typeid;

        console.log(url);
        if (n) {
            page += 1;
        } else {
            page = 1;
        }
        api.ajax({
            url: url,
            method: 'get',
            data: {
                values: {
                    p: page,
                }
            }

        }, function(ret, err) {
            if (ret) {
                // alert(JSON.stringify(ret));
                if (!ret.data) {
                    api.toast({
                        msg: '没有内容！',
                        duration: 1500,
                        location: 'middle'
                    });
                }
                // console.log(10);
                for (var i = 0; i < ret.data.length; i++) {
                    var index = getDate(ret.data[i].create_time).search(' ')
                    ret.data[i].create_time = getDate(ret.data[i].create_time).substr(0, index);
                }
                p = ret.yema;
                templateFun2(ret.data);
                api.refreshHeaderLoadDone();
                api.hideProgress();

            } else {
                alert(JSON.stringify(err));
            }
        });

    }

    function templateFun2(data) {
        var template = $api.byId('template2');
        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var list = $api.byId('list2');
        if (page == 1) {
            $api.html(list, html);
        } else {

            if (!data) {
                api.toast({
                    msg: '没有更多了',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            $api.append(list, html);
        }
        // console.log(14);
    }

    function changeType(id) {
        var li_id = 'type' + id;
        $(".li_index").removeClass("active");
        $("#" + li_id).addClass('active');
        typeid = id;
        updateData2();
    }

    function readCon(id, end_time, user_id, name, num) {

        api.openWin({
            name: 'content',
            url: './content.html',
            pageParam: {
                id: id,
                // typeid: typeid,
                end_time: end_time,
                sender_id: user_id,
                name: name,
                num: num
            }
        });

    }

    // 下拉刷新
    function down_refresh() {

        api.setRefreshHeaderInfo({
            visible: true,
            // loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...',
            showTime: true
        }, function(ret, err) {
            page = 1;

            updateData2();
        });
    }

    // 上拉加载
    function addObserver() {
        api.addEventListener({
            name: 'scrolltobottom',
            extra: {
                threshold: 0
            }
        }, function(ret, err) {

            if (p <= page) {
                api.toast({
                    msg: '没有更多了',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            updateData2(true);
        });
    }
</script>

</html>
